package com.lsyw.intergrate.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lsyw.intergrate.entity.Massif;
import com.lsyw.intergrate.entity.MassifRecord;
import com.lsyw.intergrate.service.MassifPlanAlarmService;
import org.springframework.util.StringUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.lsyw.intergrate.entity.common.PageInfo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lsyw.intergrate.common.Result;
import org.springframework.util.CollectionUtils;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import com.lsyw.intergrate.service.MassifRecordService;
import com.lsyw.intergrate.entity.MassifRecord;

import java.util.*;

import io.swagger.annotations.Api;
import lombok.Data;

import org.springframework.stereotype.Controller;

/**
 * <p>
 * 工地修复记录 前端控制器
 * </p>
 *
 * @author 刘磊
 * @since 2024-02-21
 */
@Api(tags = "工地修复记录")
@RestController
@RequestMapping("/massif-record")
public class MassifRecordController {


    @Autowired
    private MassifRecordService massifRecordService;

    /**
     * 通过id查询
     */
    @GetMapping("/get-by-id")
    public Result getById(String id) {
        return Result.successResult(massifRecordService.getById(id));
    }

    /**
     * 新增
     */
    @PostMapping("/save")
    public Result save(@RequestBody MassifRecord massifRecord) {
        massifRecord.setDelFlag(0);
        massifRecord.setGmtCreate(new Date());
        massifRecordService.save(massifRecord);
        return Result.successResult(null);
    }

    /**
     * 通过id删除
     */
    @GetMapping("/delete-by-id/{id}")
    public Result delete(@PathVariable(value = "id") String id) {
        MassifRecord massifRecord = this.massifRecordService.getById(Long.parseLong(id));
        massifRecord.setDelFlag(1);
        this.massifRecordService.updateById(massifRecord);
        return Result.successResult(null);
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public Result updateById(@RequestBody MassifRecord massifRecord) {
        massifRecord.setGmtModified(new Date());
        massifRecordService.updateById(massifRecord);
        return Result.successResult(null);
    }


    /**
     * 查询列表
     */
    @PostMapping("/list")
    public Result list(@RequestBody MassifRecordController.MassifRecordReqVo massifRecord) {
        final LambdaQueryWrapper<MassifRecord> lambda = new QueryWrapper<MassifRecord>().lambda();
        this.buildCondition(lambda, massifRecord);
        return Result.successResult(massifRecordService.list(lambda));
    }

    /**
     * 分页查询
     */
    @PostMapping("/page")
    public Result page(@RequestBody PageInfo pageInfo) {
        Page<MassifRecord> page = new Page<>();
        page.setSize(pageInfo.getPageSize());
        page.setCurrent(pageInfo.getPageNum());
        final LambdaQueryWrapper<MassifRecord> lambda = new QueryWrapper<MassifRecord>().lambda();
        //TODO 自己的查询逻辑
        Map<String, Object> condition = pageInfo.getCondition();
        if (condition.get("massifId") != null && !StringUtils.isEmpty(condition.get("massifId").toString())) {
            lambda.like(MassifRecord::getMassifId, condition.get("massifId"));
        }
        lambda.eq(MassifRecord::getDelFlag, 0);
        IPage<MassifRecord> iPage =this.massifRecordService.page(page, lambda);
        return Result.successResult(iPage);
    }


    /**
     * 构造查询条件
     *
     * @param lambda
     * @param param
     */
    private void buildCondition(LambdaQueryWrapper<MassifRecord> lambda, MassifRecordReqVo param) {
        if (!StringUtils.isEmpty(param.getId())) {
            lambda.eq(MassifRecord::getId, param.getId());
        }
        if (!CollectionUtils.isEmpty(param.getDateList())) {
            lambda.ge(MassifRecord::getDate, param.getDateList().get(0));
            lambda.le(MassifRecord::getDate, param.getDateList().get(1));
        }
        if (!StringUtils.isEmpty(param.getInitHeat())) {
            lambda.eq(MassifRecord::getInitHeat, param.getInitHeat());
        }
        if (!StringUtils.isEmpty(param.getOtherHeat())) {
            lambda.eq(MassifRecord::getOtherHeat, param.getOtherHeat());
        }
        if (!StringUtils.isEmpty(param.getKilnNum())) {
            lambda.eq(MassifRecord::getKilnNum, param.getKilnNum());
        }
        if (!StringUtils.isEmpty(param.getSoilEpair())) {
            lambda.eq(MassifRecord::getSoilEpair, param.getSoilEpair());
        }
        if (!StringUtils.isEmpty(param.getPretreatment())) {
            lambda.eq(MassifRecord::getPretreatment, param.getPretreatment());
        }
        if (!StringUtils.isEmpty(param.getGroundWater())) {
            lambda.eq(MassifRecord::getGroundWater, param.getGroundWater());
        }
        if (!StringUtils.isEmpty(param.getMassifId())) {
            lambda.eq(MassifRecord::getMassifId, param.getMassifId());
        }
        if (!StringUtils.isEmpty(param.getSort())) {
            lambda.eq(MassifRecord::getSort, param.getSort());
        }
        if (!StringUtils.isEmpty(param.getStatus())) {
            lambda.eq(MassifRecord::getStatus, param.getStatus());
        }
        if (!StringUtils.isEmpty(param.getGmtCreateUser())) {
            lambda.eq(MassifRecord::getGmtCreateUser, param.getGmtCreateUser());
        }
        if (!CollectionUtils.isEmpty(param.getGmtCreateList())) {
            lambda.ge(MassifRecord::getGmtCreate, param.getGmtCreateList().get(0));
            lambda.le(MassifRecord::getGmtCreate, param.getGmtCreateList().get(1));
        }
        if (!StringUtils.isEmpty(param.getGmtModifiedUser())) {
            lambda.eq(MassifRecord::getGmtModifiedUser, param.getGmtModifiedUser());
        }
        if (!CollectionUtils.isEmpty(param.getGmtModifiedList())) {
            lambda.ge(MassifRecord::getGmtModified, param.getGmtModifiedList().get(0));
            lambda.le(MassifRecord::getGmtModified, param.getGmtModifiedList().get(1));
        }
        lambda.eq(MassifRecord::getDelFlag, 0);
        lambda.orderBy(true, false, MassifRecord::getId);
    }


    /**
     * 请求model
     */
    @Data
    @AllArgsConstructor
    private static class MassifRecordReqVo extends MassifRecord {
        private List<String> dateList; // 日期起止
        private List<String> gmtCreateList; // 创建时间起止
        private List<String> gmtModifiedList; // 更新时间起止
    }


}
